package com.hmall.item.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hmall.common.dto.PageDTO;
import com.hmall.item.mapper.ItemMapper;
import com.hmall.item.pojo.Item;
import com.hmall.item.pojo.dto.ItemDto;
import com.hmall.item.service.IItemService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Objects;

@Service
public class ItemService extends ServiceImpl<ItemMapper, Item> implements IItemService {
    @Override
    public PageDTO<Item> findItemsByPage(Integer page, Integer size) {
        if (Objects.isNull(page) || Objects.isNull(size)){
            return new PageDTO<Item>(0L,new ArrayList<>());
        }
        //mp用分页查询一定要加上分页拦截器
        IPage<Item> iPage = page(new Page<>(page, size));
        return new PageDTO<Item>(iPage.getTotal(),iPage.getRecords());
    }

    @Override
    public void addItem(ItemDto itemDto) {
        if (Objects.isNull(itemDto)){
            return;
        }
        Item item = new Item();
        BeanUtils.copyProperties(itemDto,item);
        item.setSold(0);
        item.setCommentCount(0);
        item.setStatus(1);
        save(item);
    }
}
